package com.kadron.jobs;

import java.io.Serializable;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;

import com.kadron.common.KConst;
import com.kadron.common.KUtil;

@Startup(depends = "kadronEntityManagerFactory")
@AutoCreate
@Scope(ScopeType.APPLICATION)
@Name(KJobs.K_NAME)
public class KJobs implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -7206517981543176431L;
	
	private static final Log log = LogFactory.getLog(KJobs.class);
	public static final String K_NAME = "kJobs";
	
	@In(value = KDBConnectionJob.K_NAME)
	private KDBConnectionJob connectionJob;
	
	@In(value = KNewsletterJob.K_NAME)
	private KNewsletterJob newsletterJob;
	
	@In(value = KUserTypeUpdateJob.K_NAME)
	private KUserTypeUpdateJob userTypeUpdateJob;
	
	@In(value = KUserBirthdayJob.K_NAME)
	private KUserBirthdayJob userBirthdayJob;
	
	@In(value = KJobSheduler.K_NAME)
	private KJobSheduler jobSheduler = (KJobSheduler) Component.getInstance(KJobSheduler.K_NAME);
	
	@Create
	public void init() {
		if (log.isInfoEnabled()) {
			log.info("==== INIT JOBS ====");			
		}
		jobSheduler.shedule(connectionJob, KUtil.getMoment(), KConst.HOUR);
		jobSheduler.shedule(newsletterJob, KUtil.getMoment(), KConst.DAY);
		jobSheduler.shedule(userTypeUpdateJob, KUtil.getMoment(), KConst.DAY);
		jobSheduler.shedule(userBirthdayJob, KUtil.getMoment(), KConst.DAY);
	}
	
}
